//
//  YFImagePicker.m
//  ImagePickerDemo
//
//  Created by Winny Zhang on 2019/4/8.
//  Copyright © 2019 Winny Zhang. All rights reserved.
//

#import "YFImagePicker.h"
#import "TZImagePickerController.h"


@interface YFImagePicker ()

@end




@implementation YFImagePicker


/**
 检查是否授权

 @return 是否授权
 */
+ (BOOL)checkAuthorization {
    if (!(PHPhotoLibrary.authorizationStatus == PHAuthorizationStatusAuthorized || PHPhotoLibrary.authorizationStatus == PHAuthorizationStatusNotDetermined)) {
        return NO;
    }
    return YES;
}


/**
 多选图片

 @param maxImagesCount 最大可选图片数
 @param imagesCompletion 回调
 */
+ (void)multiplePickerWithMaxImagesCount:(NSInteger)maxImagesCount selecteImages:(NSArray *)selectImages completedWithImages:(void(^)(NSArray <UIImage *> *photos, NSArray<PHAsset *> *assets, BOOL isSelectOriginalPhoto))imagesCompletion {
    
    if (![YFImagePicker checkAuthorization]) {
        NSLog(@"相册未授权！");
        return;
    }
    
    TZImagePickerController *vc = [[TZImagePickerController alloc] initWithMaxImagesCount:maxImagesCount delegate:nil];
    vc.allowPickingVideo = NO;
    vc.iconThemeColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    vc.naviBgColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1];
    vc.oKButtonTitleColorNormal = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    vc.naviTitleColor = [UIColor blackColor];
    vc.statusBarStyle = UIStatusBarStyleDefault;
    vc.oKButtonTitleColorDisabled = [UIColor grayColor];
    vc.showPhotoCannotSelectLayer = YES;
    vc.barItemTextColor = [UIColor blackColor];
    vc.selectedAssets = [NSMutableArray arrayWithArray:selectImages];
    // 选择图片
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if (imagesCompletion) {
            imagesCompletion(photos, assets, isSelectOriginalPhoto);
        }
    }];
    
    [[UIViewController currentViewController] presentViewController:vc animated:YES completion:nil];
}

/**
 单选视频

 @param videoCompletion 回调
 */
+ (void)singlePickerVideoWithCompletion:(void(^)(UIImage *coverImage, NSString * filePath,long audioDurationSeconds))videoCompletion {
    
    if (![YFImagePicker checkAuthorization]) return;
    TZImagePickerController *vc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
    vc.allowPickingVideo = YES;
    vc.allowPickingImage = NO;
    vc.allowPickingMultipleVideo = NO;
    vc.showPhotoCannotSelectLayer = YES;
    vc.naviTitleColor = [UIColor blackColor];
    vc.statusBarStyle = UIStatusBarStyleDefault;
    vc.iconThemeColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    vc.naviBgColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1];
    vc.oKButtonTitleColorNormal = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    vc.title = @"选择视频";
    vc.oKButtonTitleColorDisabled = [UIColor blackColor];
    vc.barItemTextColor = [UIColor blackColor];
    [vc setDidFinishPickingVideoHandle:^(UIImage *coverImage, PHAsset *asset) {
        [[UIViewController currentViewController] dismissViewControllerAnimated:NO completion:^{
            if (asset.mediaType == PHAssetMediaTypeVideo) {
                PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
                options.version = PHImageRequestOptionsVersionCurrent;
                options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
                
                PHImageManager *manager = [PHImageManager defaultManager];
                [manager requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
                    AVURLAsset *urlAsset = (AVURLAsset *)asset;
                    NSURL *url = urlAsset.URL;
                    LKLog(@"%@",url.absoluteString);
                    // 获取音频时长
                    AVURLAsset* audioAsset =[AVURLAsset URLAssetWithURL:url options:nil];
                    CMTime audioDuration = audioAsset.duration;
                    long audioDurationSeconds =CMTimeGetSeconds(audioDuration);
                    if (videoCompletion) {
                        videoCompletion(coverImage, url.absoluteString,audioDurationSeconds);
                    }
                }];
            }
        }];
    }];
    
    [[UIViewController currentViewController] presentViewController:vc animated:YES completion:nil];
}


@end
